﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace KATS_Server
{
    /// <summary>
    /// A saved username and password
    /// </summary>
    class User
    {
        /// <summary>
        /// Create the user
        /// </summary>
        /// <param name="username">Username</param>
        /// <param name="password">Password</param>
        public User(string username, string password)
        {
            this.username = username;
            this.password = password;
        }

        /// <summary>
        /// Username
        /// </summary>
        public string username = string.Empty;

        /// <summary>
        /// Password
        /// </summary>
        public string password = string.Empty;

        /// <summary>Save the users to a file</summary>
        /// <param name="users"></param>
        static void Save(User[] users)
        {
            try
            {
                List<XElement> ud = new List<XElement>();
                foreach (User u in users)
                {
                        XElement xe = new XElement("User",
                             new XElement("Username", u.username),
                             new XElement("Password", u.password)
                             );
                    ud.Add(xe);
                }

                XDocument document =
                     new XDocument(
                         new XDeclaration("1.0", "uft-8", "yes"),
                         new XComment("Users"),
                         ud.ToArray()
                         );
                document.Save(Properties.Settings.Default.UsernameAndPasswordFile);
                
            }
            catch (Exception e)
            {
                // catch any errors and display them on the console
                Console.WriteLine("ERROR: Could not save user data to file\n->{0}", e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }

        /// <summary>Create a default user set and save it</summary>
        static void CreateDefault()
        {
            User[] u = new User[1];
            u[0] = new User("Admin", "Password");

            Save(u);
        }

        /// <summary>Load the users for the server from its file</summary>
        public static User[] LoadUsers()
        {
            if (!System.IO.File.Exists(Properties.Settings.Default.UsernameAndPasswordFile))
            {
                // username and password file doesnt exist
                CreateDefault();
            }
            XDocument document = XDocument.Load(Properties.Settings.Default.UsernameAndPasswordFile);
            var users = from u in document.Descendants("User") select u;

            List<User> usersList = new List<User>();

            foreach (XElement x in users)
            {
                string user = x.Element("Username").Value;
                string pass = x.Element("Password").Value;

                User userObj = new User(user, pass);
                usersList.Add(userObj);
            }

            return usersList.ToArray();
        }
    }
}
